﻿using Microsoft.EntityFrameworkCore;

namespace FrameWork.DataHelper
{
    public static class Valid
    {
        /**
         * 校验Id有效性
         */
        public static async Task<T> ValidId<T>(DbSet<T> table, string Id) where T : class
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return null;
            };

            //校验Id有效性
            var query = await table.FindAsync(Id);
            return query;
        }

        /**
         * 校验Id有效性列表
         */
        public static async Task<List<T>> ValidIdList<T>(DbSet<T> table, List<string> Ids) where T : class
        {
            //判空
            if (Ids == null || Ids.Count == 0)
            {
                return null;
            }

            var idProperty = typeof(T).GetProperty("Id");
            if (idProperty == null)
            {
                return null;
            }

            //查找id
            var query = await table
                .Where(a => Ids.Contains(idProperty.GetValue(a).ToString()))
                .ToListAsync();

            if (query.Count == 0)
            {
                return null;
            }

            return query;
        }
    }
}
